package com.ctms.system.controller;

import com.ctms.common.utils.PageUtils;
import com.ctms.common.utils.Query;
import com.ctms.common.utils.ShiroUtils;
import com.ctms.system.domain.CourseDO;
import com.ctms.system.domain.UserDO;
import com.ctms.system.service.CourseService;
import com.ctms.system.service.CourseTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RequestMapping("/myTeacherCourse")
@Controller
public class MyTeaCourseController {

    @Autowired
    CourseTeacherService courseTeacherService;
    @Autowired
    CourseService courseService;

    @GetMapping("")
    public String MyTeacherCourse(){
        return "sys/myCourse/myTeacherCourse";
    }

    @GetMapping("/list")
    @ResponseBody
    PageUtils list(@RequestParam Map<String,Object> param){
        UserDO userDO = ShiroUtils.getUser();
        int loginId=Integer.parseInt(Long.toString(userDO.getUserId()));
        param.put("loginId",loginId);
        Query query = new Query(param);
        List<CourseDO> courseDOS = courseTeacherService.selectAllTeacherCourse(param);
        if (courseDOS.size()>0){
            for (int i=0;i<courseDOS.size();i++){
                String teacherId=courseDOS.get(i).getCourseTeacher();
                String[] split=teacherId.split(",");
                String teacher="";
                for (int j=0;j<split.length;j++){
                    String teacherName = courseService.getNameById(Integer.parseInt(split[j]));
                    if (j != split.length-1){
                        teacher = teacher + teacherName + ",";
                    }else {
                        teacher = teacher + teacherName;
                    }
                }
                System.out.println(teacher);
                courseDOS.get(i).setCourseTeacher(teacher);
            }
        }
        int total = courseTeacherService.count(query);
        PageUtils pageUtils = new PageUtils(courseDOS,total);
        return pageUtils;
    }


    @GetMapping("/get/{courseName}")
    public String MyTeacherCourse(Model model,@PathVariable("courseName")String courseName){
        System.out.println("************************"+courseName+"***************");
        model.addAttribute("courseName",courseName);
        return "sys/myCourse/listSingle";
    }

    @GetMapping("/listSingle")
    @ResponseBody
    PageUtils listSingle(@RequestParam Map<String,Object> params){
        String courseName = String.valueOf(params.get("courseName"));
        System.out.println("+++++++++++++++++"+courseName);
        CourseDO courseDO = courseService.selectCourseByCid(courseName);
        String teacherId=courseDO.getCourseTeacher();
        String[] split=teacherId.split(",");
        String teacher="";
        for (int j=0;j<split.length;j++){
            String teacherName = courseService.getNameById(Integer.parseInt(split[j]));
            if (j != split.length-1){
                teacher = teacher + teacherName + ",";
            }else {
                teacher = teacher + teacherName;
            }
        }
        System.out.println(teacher);
        courseDO.setCourseTeacher(teacher);
        List<CourseDO> courseDOS = new ArrayList<CourseDO>();
        courseDOS.add(courseDO);
        int total = courseDOS.size();
        PageUtils pageUtils = new PageUtils(courseDOS,total);
        return pageUtils;
    }

}
